package com.tinywind.admin.module.system.service.impl;

import com.tinywind.admin.common.config.shiro.ShiroUtils;
import com.tinywind.admin.common.consts.SystemConst;
import com.tinywind.admin.common.utils.RedisUtil;
import com.tinywind.admin.module.system.mapper.SysUserRoleMapper;
import com.tinywind.admin.module.system.service.SysUserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author liuxingyu01
 * @date 2021-12-23-13:26
 **/
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

    final static Logger logger = LoggerFactory.getLogger(SysUserRoleServiceImpl.class);

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Set<String> listUserRoleByUserId(String userId) {
        String token = ShiroUtils.getSessionId();

        Object object = redisUtil.get(SystemConst.SYSTEM_USER_ROLE + ":" + token);
        if (null != object) {
            return (Set<String>) object;
        }
        Set<String> set = sysUserRoleMapper.listUserRoleByUserId(userId);
        redisUtil.set(SystemConst.SYSTEM_USER_ROLE + ":" + token, set, 1800);

        return set;
    }


}
